﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using eCommerce.core.Helpers;
using eCommerce.db.BORespository;
using System.Configuration;
using eCommerce.core;

namespace eCommerce.web.Controls.Product
{
    public partial class Related : System.Web.UI.UserControl
    {
        public int CategoryID { get; set; }
        public int ProductID { get; set; }
        protected int TOP = 4;
        private string name_request;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                name_request = Utils.GetIdFromRequestString(Utils.Name, string.Empty);
                var relateProduct = ProductBo.GetRelateProduct(TOP,ProductID, CategoryID);
                if (relateProduct.Count > 0)
                {
                    rptProduct.DataSource = relateProduct;
                    rptProduct.DataBind();
                }
                else
                    this.Visible = false;
            }
        }

        protected void rptProduct_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                Literal ltrHref = (Literal)e.Item.FindControl("ltrHref");
                Literal ltrHrefBelow = (Literal)e.Item.FindControl("ltrHrefBelow");
                Literal ltrIconNew = (Literal)e.Item.FindControl("ltrIconNew");

                var productId = Utils.ToInt32(((HiddenField)e.Item.FindControl("hfProductId")).Value);
                var name = Utils.UnsignAndDash(((HiddenField)e.Item.FindControl("hfName")).Value);
                var imagePath = ((HiddenField)e.Item.FindControl("hdImagePath")).Value;
                var status = ((HiddenField)e.Item.FindControl("hdStatus")).Value;

                var ltImage = ((Literal)e.Item.FindControl("ltImage"));

                string Img1 = ConfigurationManager.AppSettings["Thumbnail"] + string.Format("/Thumb.ashx?imgPath={0}&tid={1}&size=200", imagePath, (int)SettingType.Product);
                ltImage.Text = "<img src=\"" + Img1 + "\" />";
                ltrHref.Text = ltrHrefBelow.Text = "<a href=\" " + UrlHelper.ProductDetail(Utils.UnsignAndDash(name_request), productId, name) + " \">";

                if (status == ConfigurationManager.AppSettings["IsNew"])
                    ltrIconNew.Text = "<p class=\"new\">New</p>";
            }
        }
    }
}